package com.tiancheng.trade.order.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.tiancheng.trade.commom.core.data.PageInfo;
import com.tiancheng.trade.commom.core.data.notify.PayResultMsgBO;
import com.tiancheng.trade.commom.core.exception.BusinessException;
import com.tiancheng.trade.order.bo.*;
import com.tiancheng.trade.order.bo.cmq.ScrmOrder;
import com.tiancheng.trade.order.bo.notify.PayResultHandlerResultBO;
import com.tiancheng.trade.order.model.Order;
import com.tiancheng.trade.order.model.join.OrderJoin;
import com.tiancheng.trade.order.vo.admin.QueryOrdersVO;
import com.tiancheng.trade.order.vo.admin.QueryTotalOrderVO;
import com.tiancheng.trade.order.vo.admin.res.OrderDetailResVO;
import com.tiancheng.trade.order.vo.admin.res.OrderResVO;
import com.tiancheng.trade.order.vo.api.CloseOrderVO;
import com.tiancheng.trade.order.vo.api.OrderVO;
import com.tiancheng.trade.order.vo.api.ScrmOrderQueryVO;
import com.tiancheng.trade.order.vo.api.UpdateOrderVO;
import com.tiancheng.trade.order.vo.api.res.CreateOrderResVO;
import com.tiancheng.trade.order.vo.api.res.OrderDiscountInfoVO;
import com.tiancheng.trade.order.vo.app.CashierVO;
import com.tiancheng.trade.order.vo.app.OrderCenterQueryVO;
import com.tiancheng.trade.order.vo.app.OrderQueryVO;
import com.tiancheng.trade.order.vo.app.res.OrderPrePayResVO;
import com.tiancheng.trade.order.vo.app.res.OrderQueryResVO;
import com.tiancheng.trade.order.vo.open.order.OpenCashierResVO;
import com.tiancheng.trade.order.vo.open.order.OpenOrderPayVO;
import com.tiancheng.trade.order.vo.parallel.AppleInAppPayVO;
import com.tiancheng.trade.order.vo.parallel.res.AppleInAppPayResVO;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 订单Service接口
 *
 * @author kellen
 * @date 2024-03-02
 */
public interface OrderService extends IService<Order> {

    /**
     * 根据主键id查询主订单详情
     *
     * @param orderId
     * @return
     */
    OrderDetailResVO queryOrderDetailsByOrderId(String orderId);


    /**
     * 主订单查询分页
     *
     * @param orderVO
     * @return
     */
    PageInfo<OrderResVO> queryOrderList(QueryOrdersVO orderVO);

    /**
     * 总订单导出
     */
    void exportOrderList(QueryOrdersVO orderVO, HttpServletResponse response) throws IOException;

    /**
     * 获取订单的支付渠道，并检查是否跨渠道支付
     *
     * @param subOrderBOS
     * @param orderVO 订单信息
     * @return
     * @throws BusinessException
     */
    String getPayChannelAndSetMerchant(List<SubOrderBO> subOrderBOS, OrderVO orderVO) throws BusinessException;

    /**
     * 创建订单（主订单、子订单、优惠券）
     *
     * @param orderBO
     * @param subOrderBOS
     * @param couponBOS
     * @param vipBO 会员信息
     * @return
     * @throws BusinessException
     */
    String create(OrderBO orderBO, List<SubOrderBO> subOrderBOS, List<CouponBO> couponBOS,VipBO vipBO) throws BusinessException;
    /**
     * 创建订单（主订单、子订单、优惠券）
     *
     * @param orderBO
     * @param subOrderBOS
     * @param couponBOS
     * @param vipBO 会员信息
     * @return
     * @throws BusinessException
     */
    CreateOrderResVO createOrder(OrderBO orderBO, List<SubOrderBO> subOrderBOS, List<CouponBO> couponBOS,VipBO vipBO) throws BusinessException;

    /**
     * 创建抵扣订单订单（主订单、子订单、优惠券）
     *
     * @param orderBO
     * @param subOrderBOS
     * @return
     * @throws BusinessException
     */
    String createDeductionOrder(OrderBO orderBO, List<SubOrderBO> subOrderBOS) throws BusinessException;

    /**
     * 支付订单
     *
     * @param uid
     * @param orderPayVO
     * @return
     * @throws BusinessException
     */
    OrderPrePayResVO pay(String uid, OpenOrderPayVO orderPayVO) throws BusinessException;

    /**
     * 支付订单——零元抵扣支付
     *
     * @param uid
     * @param orderPayVO
     * @return
     * @throws BusinessException
     */
    Boolean zeroYuanDeductionPay(String uid, OpenOrderPayVO orderPayVO) throws BusinessException;

    /**
     * 保存支付通知结果
     *
     * @param payNotifyBO
     * @throws BusinessException
     */
    PayResultHandlerResultBO savePayResult(PayResultMsgBO payNotifyBO) throws BusinessException;

    /**
     * 初始化收银台
     *
     * @param uid
     * @param cashierVO
     * @return
     */
    OpenCashierResVO initCashier(String uid, CashierVO cashierVO) throws BusinessException;

    /**
     * 修改订单金额信息
     * @param updateOrderVO
     * @throws BusinessException
     */
    void updateAmount(UpdateOrderVO updateOrderVO) throws BusinessException;

    /**
     * 根据订单号查询
     *
     * @param orderId
     * @return
     */
    Order get(String orderId);

    /***
     * 移动端查询订单数据
     * @param orderQueryVO
     * @return
     */
    PageInfo<OrderQueryResVO> findForApp(OrderQueryVO orderQueryVO);

    /**
     * 查询
     *
     * @param uid
     * @param orderIds
     * @return
     */
    List<Order> find(String uid, String[] orderIds);

    /**
     * 查询指定订单
     *
     * @param uid
     * @param orderId
     * @return
     */
    Order get(String uid, String orderId);

    /**
     * 关闭订单
     *
     * @param closeOrderVO
     * @return
     * @throws BusinessException
     */
    boolean closeOrder(CloseOrderVO closeOrderVO) throws BusinessException;

    /**
     * 退款
     *
     * @param success
     * @param orderId
     * @param subOrderRefundBOS
     * @return
     */
    int refund(boolean success, String orderId, List<SubOrderRefundBO> subOrderRefundBOS);

    /**
     * 查询统计订单数量、总金额
     *
     * @param queryTotalOrderVO
     * @return
     */
    Map<String, Long> queryTotal(QueryTotalOrderVO queryTotalOrderVO);

    /**
     * 按照业务渠道统计订单的金额和数量
     *
     * @param queryTotalOrderVO
     * @return
     */
    List<Map<String, Object>> queryTotalForSource(QueryTotalOrderVO queryTotalOrderVO);

    /**
     * 根据流水号查询主订单及子订单数据
     *
     * @param flowId
     * @return
     */
    List<OrderJoin> findWithSubByFlowId(String flowId);
    /**
     * 根据流水号查询主订单及子订单数据
     *
     * @param flowId
     * @return
     */
    Order findByFlowId(String flowId);
    /**
     * 根据订单号查询主订单及子订单数据
     *
     * @param orderId
     * @return
     */
    OrderJoin findByOrderId(String orderId);

    /**
     * 获取订单的支付状态
     *
     * @param uid
     * @param orderId
     * @return
     */
    String getPayStatus(String uid, String orderId) throws BusinessException;

    /**
     * 个人中心展示服务中订单查询
     *
     * @param queryVO {@link OrderCenterQueryVO}
     * @return {@link PageInfo}
     */
    PageInfo<OrderQueryResVO> getCenterOrder(OrderCenterQueryVO queryVO);
    /**
     * 根据主订号查询单
     *
     * @param orderIds
     * @return
     */
    List<Order> findByOrderIds(Set<String> orderIds);

    /**
     * 苹果支付
     * @param appleInAppPayVO
     * @return
     */
    AppleInAppPayResVO appleInAppPay(AppleInAppPayVO appleInAppPayVO);

    /**
     * 苹果支付——同步校验
     * @param appleInAppPayVO
     * @return
     */
    AppleInAppPayResVO appleInAppPayNew(AppleInAppPayVO appleInAppPayVO);

    PageInfo<ScrmOrder> queryPayOrderAndSubOrder(ScrmOrderQueryVO orderVO);

    /**
     * 通过子订单号查询
     * @param subOrderIds
     * @return
     */
    List<ScrmOrder> queryScrmOrderBySubOrders(Collection<String> subOrderIds);

    /**
     * 查询订单优惠信息
     * @param orderId
     * @return
     */
    OrderDiscountInfoVO getOrderDiscountInfo(String orderId);

    List<Order> findByUidAndBatchId(String uid,String batchId);
}
